//
//  BottomSelectView.swift
//  PhotoTestDemo
//
//  Created by 惠 on 2020/9/8.
//  Copyright © 2020 惠. All rights reserved.
//

import UIKit

class BottomSelectView: UIView {
    
    let screenW = UIScreen.main.bounds.width
    let screenH = UIScreen.main.bounds.height
    //最多选择图片数量
    var maxSelectPhotoCount: Int? {
        didSet {
            totalCount.text = "/\(maxSelectPhotoCount ?? 9))"
        }
    }
    
    private var colorType: ColorType!
    init(colorType: ColorType = .defaults, frame: CGRect = .zero) {
        super.init(frame: frame)
        
        self.colorType = colorType
        setUI()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    private func setUI() {
        backgroundColor = .white
        
        addSubview(swicthBottomView)
        swicthBottomView.addSubview(photoButton)
        swicthBottomView.addSubview(videoButton)
        swicthBottomView.addSubview(takingPicturesButton)
        swicthBottomView.addSubview(lineView)
        
        addSubview(selectBottomView)
        selectBottomView.addSubview(selectLabel)
        selectBottomView.addSubview(selectedCount)
        selectBottomView.addSubview(totalCount)
        selectBottomView.addSubview(nextButton)
        
        addSubview(alphaButton)
    }
    
//MARK: -懒加载UI
    //切换视图
    lazy var swicthBottomView: UIView = {
        let swicthBottomView = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: screenW, height: 50)))
        swicthBottomView.backgroundColor = .clear
        return swicthBottomView
    }()
    lazy var photoButton: UIButton = {
        let photoButton = UIButton(type: .custom)
        photoButton.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: screenW/3, height: 40))
        photoButton.setTitle("相册", for: .normal)
        photoButton.setTitleColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1), for: .selected)
        photoButton.setTitleColor(UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1), for: .normal)
        photoButton.titleLabel?.font = UIFont.init(name: "Helvetica-Bold", size: 16)
        photoButton.isSelected = true
        photoButton.addTarget(self, action: #selector(photoButtonAction(sender:)), for: .touchUpInside)
        return photoButton
    }()
   
    lazy var videoButton: UIButton = {
        let videoButton = UIButton(type: .custom)
        videoButton.frame = CGRect(origin: CGPoint(x: screenW/3, y: 0), size: CGSize(width: screenW/3, height: 40))
        videoButton.setTitle("拍视频", for: .normal)
        videoButton.setTitleColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1), for: .selected)
        videoButton.setTitleColor(UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1), for: .normal)
        videoButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        videoButton.isSelected = false
        videoButton.addTarget(self, action: #selector(videoButtonAction(sender:)), for: .touchUpInside)
        return videoButton
    }()
    
    lazy var takingPicturesButton: UIButton = {
        let takingPicturesButton = UIButton(type: .custom)
        takingPicturesButton.frame = CGRect(origin: CGPoint(x: screenW*2/3, y: 0), size: CGSize(width: screenW/3, height: 40))
        takingPicturesButton.setTitle("拍照", for: .normal)
        takingPicturesButton.setTitleColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1), for: .selected)
        takingPicturesButton.setTitleColor(UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1), for: .normal)
        takingPicturesButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        takingPicturesButton.isSelected = false
        takingPicturesButton.addTarget(self, action: #selector(takingPicturesButtonAction(sender:)), for: .touchUpInside)
        return takingPicturesButton
    }()
    
    lazy var lineView: UIView = {
        let lineView = UIView(frame: CGRect(origin: CGPoint(x: (screenW/3-20)/2, y: 35), size: CGSize(width: 20, height: 3)))
        switch colorType {
        case .defaults:
            lineView.backgroundColor = UIColor(red: 0.25, green: 0.76, blue: 0.8, alpha: 1)
        case .red:
            lineView.backgroundColor = UIColor(red: 0.77, green: 0.16, blue: 0.16, alpha: 1)
        case .none:
            lineView.backgroundColor = UIColor(red: 0.25, green: 0.76, blue: 0.8, alpha: 1)
        }
        lineView.layer.masksToBounds = true
        lineView.layer.cornerRadius = 1.5
        return lineView
    }()
    
    //选中图片
    lazy var selectBottomView: UIView = {
        let selectBottomView = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: screenW, height: 50)))
        selectBottomView.backgroundColor = .white
        selectBottomView.isHidden = true
        return selectBottomView
    }()
    
    //选择数量
    lazy var selectLabel: UILabel = {
        let selectLabel = UILabel(frame: CGRect(origin: CGPoint(x: 25, y: 0), size: CGSize(width: 55, height: 40)))
        selectLabel.text = "已选择 ("
        selectLabel.textColor = .black
        selectLabel.textAlignment = .left
        selectLabel.font = UIFont.systemFont(ofSize: 14)
        return selectLabel
    }()
    lazy var selectedCount: UILabel = {
        let selectedCount = UILabel(frame: CGRect(origin: CGPoint(x: 78, y: 0), size: CGSize(width: 8, height: 40)))
        selectedCount.text = "0"
        switch colorType {
        case .defaults:
            selectedCount.textColor = UIColor(red: 0.25, green: 0.76, blue: 0.8, alpha: 1)
        case .red:
            selectedCount.textColor = UIColor(red: 0.77, green: 0.16, blue: 0.16, alpha: 1)
        case .none:
            selectedCount.textColor = UIColor(red: 0.25, green: 0.76, blue: 0.8, alpha: 1)
        }
        selectedCount.textAlignment = .left
        selectedCount.font = UIFont.systemFont(ofSize: 14)
        return selectedCount
    }()
    
    lazy var totalCount: UILabel = {
        let totalCount = UILabel(frame: CGRect(origin: CGPoint(x: 88, y: 0), size: CGSize(width: 20, height: 40)))
//        totalCount.text = "/9)"
        totalCount.textColor = .black
        totalCount.textAlignment = .left
        totalCount.font = UIFont.systemFont(ofSize: 14)
        return totalCount
    }()
    
    lazy var nextButton: UIButton = {
        let nextButton = UIButton(type: .custom)
        nextButton.frame = CGRect(origin: CGPoint(x: screenW-85, y: 13), size: CGSize(width: 60, height: 24))
        nextButton.setTitle("下一步", for: .normal)
        nextButton.setTitleColor(.white, for: .normal)
        nextButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        switch colorType {
        case .defaults:
            nextButton.backgroundColor = UIColor(red: 0.25, green: 0.76, blue: 0.8, alpha: 1)
        case .red:
            nextButton.backgroundColor = UIColor(red: 0.77, green: 0.16, blue: 0.16, alpha: 1)
        case .none:
            nextButton.backgroundColor = UIColor(red: 0.25, green: 0.76, blue: 0.8, alpha: 1)
        }
        nextButton.layer.cornerRadius = 12
        nextButton.layer.masksToBounds = true
        return nextButton
    }()
    
    //半透明视图
    lazy var alphaButton: UIButton = {
        let alphaButton = UIButton(type: .custom)
        alphaButton.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: screenW, height: 50))
        alphaButton.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7)
        alphaButton.isHidden = true
        return alphaButton
    }()
    
//MARK: -点击事件
    @objc func photoButtonAction(sender: UIButton) {
        if photoButton.isSelected == false {
            photoButton.isSelected = true
            videoButton.isSelected = false
            takingPicturesButton.isSelected = false
            
            photoButton.setTitleColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1), for: .selected)
            photoButton.titleLabel?.font = UIFont.init(name: "Helvetica-Bold", size: 16)
            videoButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            videoButton.setTitleColor(UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1), for: .normal)
            takingPicturesButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            takingPicturesButton.setTitleColor(UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1), for: .normal)
            
            lineView.frame = CGRect(origin: CGPoint(x: (screenW/3-20)/2, y: 35), size: CGSize(width: 20, height: 3))
            
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "bottonSelect"), object: self, userInfo: ["index": 0])
            bottomClearColor(index: 0)
        }
    }
    
    @objc func videoButtonAction(sender: UIButton) {
        if videoButton.isSelected == false {
            photoButton.isSelected = false
            videoButton.isSelected = true
            takingPicturesButton.isSelected = false
            
            photoButton.setTitleColor(.white, for: .normal)
            photoButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            videoButton.titleLabel?.font = UIFont.init(name: "Helvetica-Bold", size: 16)
            videoButton.setTitleColor(.white, for: .selected)
            takingPicturesButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            takingPicturesButton.setTitleColor(.white, for: .normal)
            
            lineView.frame = CGRect(origin: CGPoint(x: (screenW/3-20)/2+screenW/3, y: 35), size: CGSize(width: 20, height: 3))
            
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "bottonSelect"), object: self, userInfo: ["index": 1])
            bottomClearColor(index: 1)
        }
    }
    
    @objc func takingPicturesButtonAction(sender: UIButton) {
        if takingPicturesButton.isSelected == false {
            photoButton.isSelected = false
            videoButton.isSelected = false
            takingPicturesButton.isSelected = true
            
            photoButton.setTitleColor(.white, for: .normal)
            photoButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            videoButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            videoButton.setTitleColor(.white, for: .normal)
            takingPicturesButton.titleLabel?.font = UIFont.init(name: "Helvetica-Bold", size: 16)
            takingPicturesButton.setTitleColor(.white, for: .selected)
            
            lineView.frame = CGRect(origin: CGPoint(x: (screenW/3-20)/2+screenW*2/3, y: 35), size: CGSize(width: 20, height: 3))
            
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "bottonSelect"), object: self, userInfo: ["index": 2])
            bottomClearColor(index: 2)
        }
    }
    
    func bottomClearColor(index: Int) {
        if index == 0 {
            backgroundColor = .white
        } else {
            backgroundColor = .clear
        }
    }
}
